package com.zhdl.modules.common.formula.service.impl;

import com.project.common.service.impl.BaseServiceImpl;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.zhdl.modules.common.formula.entity.FormulaDictionary;
//import com.zhdl.modules.common.formula.entity.QFormulaDictionary;
import com.zhdl.modules.common.formula.repository.FormulaDictionaryDemoRepository;
import com.zhdl.modules.common.formula.service.FormulaDictionaryService;
import com.zhdl.modules.common.formula.web.req.FormulaListRequest;
import com.zhdl.modules.common.formula.web.resp.FormulaDictionaryListResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class FormulaDictionaryServiceImpl extends BaseServiceImpl<Integer, FormulaDictionary, FormulaDictionaryDemoRepository> implements FormulaDictionaryService {

    @Resource
    private FormulaDictionaryDemoRepository formulaRepository;


    /**
     * 添加一条套餐字典信息
     *
     * @param formulaDictionary 字段-值-类型
     */
    public void addFormulaDictionary(FormulaDictionary formulaDictionary) {
        if (formulaDictionary != null) {
            if (formulaDictionary.getId() == null) {
                FormulaDictionary save = formulaRepository.save(formulaDictionary);
            }
        }
    }

    /**
     * 更新一条套餐字典信息
     *
     * @param formulaDictionary ID-字段-值-类型
     */
    public void updateFormulaDictionary(FormulaDictionary formulaDictionary) {
        if (formulaDictionary != null) {
            if (formulaDictionary.getId() != null) {
                FormulaDictionary save = formulaRepository.save(formulaDictionary);
            }
        }
    }

    /**
     * 查询某一条字典记录
     *
     * @param id 字典Id
     * @return 查询结果/空
     */
    public FormulaDictionary findFormulaDictionaryById(Integer id) {
        Optional<FormulaDictionary> byId = formulaRepository.findById(id);
        return byId.orElse(new FormulaDictionary());
    }

    /**
     * 查询所有字典记录
     *
     * @return List<></>
     */
    public List<FormulaDictionary> findAllFormulaDictionary() {
        return formulaRepository.findAll();
    }

    /**
     * 分页查询记录
     *
     * @param request  条件查询参数
     * @param pageable 分页参数
     * @return 分页结果
     */
//    public Page<FormulaDictionaryListResp> findAllFDPage(FormulaListRequest request, Pageable pageable) {
//
//        QFormulaDictionary qFormulaDictionary = QFormulaDictionary.formulaDictionary;
//
//        BooleanBuilder builder = new BooleanBuilder();
//        builder.and(qFormulaDictionary.createDate.between(request.getStartTime(), request.getEndTime()));
//
//        if (!request.getType().isEmpty()) {
//            BooleanBuilder builder1 = new BooleanBuilder();
//            for (Integer i : request.getType()) {
//                builder1.or(qFormulaDictionary.type.eq(i));
//            }
//            builder.and(builder1);
//        }
//
//
//        JPAQuery<FormulaDictionaryListResp> query = jpaQueryFactory()
//                .select(Projections.constructor(FormulaDictionaryListResp.class,
//                        qFormulaDictionary.id,
//                        qFormulaDictionary.name,
//                        qFormulaDictionary.value,
//                        qFormulaDictionary.type
//                ))
//                .from(qFormulaDictionary)
//                .where(builder);
//
//        List<FormulaDictionaryListResp> fetch = query
//                .orderBy(qFormulaDictionary.createDate.desc())
//                .offset(pageable.getOffset())
//                .limit(pageable.getPageSize())
//                .fetch();
//
//        long total = query.fetchCount();
//
//        return new PageImpl<>(fetch, pageable, total);
//    }

}
